package com.example.cdctest.listener;

import com.example.cdctest.pojo.Config;
import com.example.cdctest.service.ConfigService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class ConfigListner implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext=sce.getServletContext();
        ApplicationContext applicationContext= WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ConfigService configService=applicationContext.getBean(ConfigService.class);
        List<Config> configs=configService.findAll();
        servletContext.setAttribute("configs",configs);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
